package cn.stylefeng.guns.modular.goods.info.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.goods.info.entity.Info;
import cn.stylefeng.guns.modular.goods.info.mapper.InfoMapper;
import cn.stylefeng.guns.modular.goods.info.model.params.InfoParam;
import cn.stylefeng.guns.modular.goods.info.model.result.InfoResult;
import  cn.stylefeng.guns.modular.goods.info.service.InfoService;
import cn.stylefeng.roses.core.datascope.DataScope;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SYT
 * @since 2020-10-13
 */
@Service
public class InfoServiceImpl extends ServiceImpl<InfoMapper, Info> implements InfoService {

    @Override
    public void add(InfoParam param){
        Info entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(InfoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(InfoParam param){
        Info oldEntity = getOldEntity(param);
        Info newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public InfoResult findBySpec(InfoParam param){
        return null;
    }

    @Override
    public List<InfoResult> findListBySpec(InfoParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(InfoParam param){
        Page pageContext = getPageContext();
        DataScope dataScope = new DataScope(LoginContextHolder.getContext().getDeptDataScope());
        IPage page = this.baseMapper.customPageList(dataScope,pageContext,param);
        return LayuiPageFactory.createPageInfo(page);
    }
    @Override
    public InfoResult getOneByID(@Param("id")Long id){
        return baseMapper.getOneByID(id);
    }

    private Serializable getKey(InfoParam param){
        return param.getGoodsId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Info getOldEntity(InfoParam param) {
        return this.getById(getKey(param));
    }

    private Info getEntity(InfoParam param) {
        Info entity = new Info();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
